package commandAction;

import java.awt.KeyboardFocusManager;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;

import com.supermap.desktop.Application;

import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.tester.JFileChooserTester;
import abbot.tester.Robot;
import command.Command;

public class ActionJFileChooser extends Action {

	@Override
	public Object action(Command cmd) {
		// TODO Auto-generated method stub
		JDialog dialog = (JDialog) KeyboardFocusManager
				.getCurrentKeyboardFocusManager().getFocusedWindow();
		JFileChooser fileChooser = null;
		try {
			fileChooser = (JFileChooser) new BasicFinder().find(dialog,
					new ClassMatcher(JFileChooser.class));
		} catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JFileChooserTester chooserTester = new JFileChooserTester();
		File file = new File(cmd.getMenuItem());
		if(!file.exists()){
			Application.getActiveApplication().getOutput().output("文件不存在："+file);
		}
		chooserTester.actionSetSelectedFile(fileChooser, file);
		chooserTester.actionSetFilename(fileChooser, file.getName());
		Robot.delay(1000);
		chooserTester.actionApprove(fileChooser);

		return null;
	}

}
